﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Meeting.DBUtility;
using Newtonsoft.Json;
using System.Data.SqlClient;
using Meeting.Model;

namespace Meeting.BLL
{
    /// <summary>
    /// 模块作者：许文亭
    /// 模块日期：2013-1-2
    /// 模块功能：会议室表Room 增删查改
    /// </summary>
    public class Room
    {
        public string errorString = "";

        //获取会议室表数据
        private DataSet GetRooms(string filter)
        {
            string sql = "Select r.*,d.Name as DepartmentName From dbo.Room as r inner join Department as d on r.DepartmentId = d.Id Where 1=1 ";
            if (!string.IsNullOrEmpty(filter)) sql += " And " + filter;
            try
            {
                return DbHelperSQL.Query(sql);
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return null;
            }
        }

        /// <summary>
        /// 获取单个实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetBy(int id)
        {
            try
            {
                DataSet ds = GetRooms(" r.Id=" + id);
                if (ds == null || ds.Tables[0].Rows.Count == 0) return "";
                Model.Room model = new Model.Room();
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Position"] != null && ds.Tables[0].Rows[0]["Position"].ToString() != "")
                {
                    model.Position = ds.Tables[0].Rows[0]["Position"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PeopleCount"] != null && ds.Tables[0].Rows[0]["PeopleCount"].ToString() != "")
                {
                    model.PeopleCount = int.Parse(ds.Tables[0].Rows[0]["PeopleCount"].ToString());
                }

                if (ds.Tables[0].Rows[0]["Comment"] != null && ds.Tables[0].Rows[0]["Comment"].ToString() != "")
                {
                    model.Comment = ds.Tables[0].Rows[0]["Comment"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsActive"] != null && ds.Tables[0].Rows[0]["IsActive"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsActive"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsActive"].ToString().ToLower() == "true"))
                    {
                        model.IsActive = true;
                    }
                    else
                    {
                        model.IsActive = false;
                    }
                }
                return JsonConvert.SerializeObject(model);
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return "";
            }
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public string GetPage(int pageIndex, int pageSize, string filter)
        {
            int nAllRecord = 0, nAllPages = 0;

            // DataTable dt = DbHelperSQL.GetPage("id", " * ", "rssNews", "", " pubDate Desc ", pageIndex, pageSize, out nAllRecord, out nAllPages);
            DataTable dt = DbHelperSQL.GetPage(" Id", "  *  ", " dbo.Room   ", filter, " Id ", pageIndex, pageSize, out nAllRecord, out nAllPages);
            if (dt == null) return "";
            JsonGrid jr = new JsonGrid();
            jr.total = nAllRecord;
            jr.rows = dt;
            return JsonConvert.SerializeObject(jr);
        }

        /// <summary>
        /// 保存会议室信息
        /// </summary>
        public int Add(Model.Room model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into Room(");
                strSql.Append("Name,Position,PeopleCount,Comment,IsActive)");
                strSql.Append(" values (");
                strSql.Append("@Name,@Position,@PeopleCount,@Comment,@IsActive)");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,128),
					new SqlParameter("@Position", SqlDbType.NVarChar,256),
					new SqlParameter("@PeopleCount", SqlDbType.Int,4),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@IsActive", SqlDbType.Bit,1)};
                parameters[0].Value = model.Name;
                parameters[1].Value = model.Position;
                parameters[2].Value = model.PeopleCount;
                parameters[3].Value = model.Comment;
                parameters[4].Value = model.IsActive;

                int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                if (rows == 0)
                {
                    return 0;
                }
                else
                {
                    return rows;
                }
            }
            catch (Exception ex) { errorString = ex.Message; return 0; }

        }

        public Boolean Update(Model.Room model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Room set ");
            strSql.Append("Name=@Name,");
            strSql.Append("Position=@Position,");
            strSql.Append("PeopleCount=@PeopleCount,");
            strSql.Append("Comment=@Comment,");
            strSql.Append("IsActive=@IsActive");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,128),
					new SqlParameter("@Position", SqlDbType.NVarChar,256),
					new SqlParameter("@PeopleCount", SqlDbType.Int,4),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@IsActive", SqlDbType.Bit,1),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Position;
            parameters[2].Value = model.PeopleCount;
            parameters[3].Value = model.Comment;
            parameters[4].Value = model.IsActive;
            parameters[5].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Room ");
            strSql.Append(" where Id in (" + ids + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
